package org.jeecg.modules.demo.applyinfo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import liquibase.pro.packaged.L;
import org.jeecg.modules.demo.applyapartemployee.entity.ZnzfApplyApartEmployee;
import org.jeecg.modules.demo.applyapartemployee.mapper.ZnzfApplyApartEmployeeMapper;
import org.jeecg.modules.demo.applyinfo.entity.ZnzfApply;
import org.jeecg.modules.demo.applyinfo.entity.ZnzfDetermineApart;
import org.jeecg.modules.demo.applyinfo.mapper.ZnzfApplyMapper;
import org.jeecg.modules.demo.applyinfo.mapper.ZnzfDetermineApartMapper;
import org.jeecg.modules.demo.applyinfo.service.IZnzfApplyService;
import org.jeecg.modules.demo.applyinfo.vo.DetermineApartVo;
import org.jeecg.modules.demo.base.entity.ZnzfEmployee;
import org.jeecg.modules.demo.base.mapper.ZnzfEmployeeMapper;
import org.jeecg.modules.system.entity.SysUserDepart;
import org.jeecg.modules.system.mapper.SysUserDepartMapper;
import org.jeecg.modules.system.vo.SysDepartUsersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 报名信息
 * @Author: jeecg-boot
 * @Date:   2022-04-11
 * @Version: V1.0
 */
@Service
public class ZnzfApplyServiceImpl extends ServiceImpl<ZnzfApplyMapper, ZnzfApply> implements IZnzfApplyService {
    @Autowired
    private ZnzfDetermineApartMapper znzfDetermineApartMapper;

    @Autowired
    private ZnzfApplyMapper znzfApplyMapper;

    @Autowired
    private SysUserDepartMapper sysUserDepartMapper;

    @Autowired
    private ZnzfEmployeeMapper znzfEmployeeMapper;

    /**
     * 用户报名提示的信息:每句话以&隔开
     * 当前分数：88分，可以报名。
     * <p>
     * 部门不一致，请确认所在部门是否参与本次报名。
     * <p>
     * 截止报名日期：2022-3-15
     * <p>
     * 当前日期：2022-3-16
     * <p>
     * 已截止报名。
     *
     * @param applyId
     * @param uid
     * @return
     */
    @Override
    public JSONObject applyMessage(String applyId, String uid) {
        JSONObject result = new JSONObject();
        result.put("canApply",false);
        result.put("message","");
        StringBuilder msg = new StringBuilder();
        //1.查询分数,分数是否达到要求
        LambdaQueryWrapper<ZnzfEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployee::getUserId, uid);
        ZnzfEmployee employee = znzfEmployeeMapper.selectOne(queryWrapper);
        String score = null;
        if (employee != null) {
            score = employee.getLatestScore().toString();
        }
        if (score != null) {
            msg.append("当前分数：" + score + "分,满足分数条件。&");
        } else {
            msg.append("暂无分数。&");
        }
        //2.判断部门是否属于框定部门
        //获取用户部门id
        List<SysUserDepart> userDeparts = sysUserDepartMapper.getUserDepartByUid(uid);
        if(userDeparts.size() <= 0){
            msg.append("用户没有部门信息。");
            result.put("message",msg.toString());
            return result;
        }

        LambdaQueryWrapper<ZnzfDetermineApart> wrapper = new LambdaQueryWrapper<ZnzfDetermineApart>();
        wrapper.eq(ZnzfDetermineApart::getApplyListId,applyId).eq(ZnzfDetermineApart::getApartId,userDeparts.get(0).getDepId());
        ZnzfDetermineApart determineApart = znzfDetermineApartMapper.selectOne(wrapper);
        if(determineApart == null){
            msg.append("部门不一致，请确认所在部门是否参与本次报名。");
            result.put("message",msg.toString());
            return result;
        }
        msg.append("满足框定部门条件。&");
        //3.是否已过截止日期
        LambdaQueryWrapper<ZnzfApply> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(ZnzfApply::getId,applyId);
        ZnzfApply znzfApply = znzfApplyMapper.selectOne(wrapper1);
        Date currDate = new Date();
        if(znzfApply.getApplyEndTime().compareTo(currDate) < 0){
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            msg.append("截止报名日期:").append(sdf.format(znzfApply.getApplyEndTime())).append("&").append("当前日期:").append(sdf.format(currDate)).append("&").append("已截止报名。");
            result.put("message",msg.toString());
            return result;
        }
        msg.append("满足报名时间条件。");
        result.put("canApply",true);
        result.put("message",msg.toString());
        return result;
    }

    @Override
    public ZnzfApply searchByProjectId(String projectId) {
        LambdaQueryWrapper<ZnzfApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfApply::getProject, projectId);
        return znzfApplyMapper.selectOne(queryWrapper);
    }
}
